import { makeAutoObservable, } from "mobx";
import { showError, } from "../components/Message";
import { ResearchDetailData, ResearchDetailService, ResearchFollowService, ResearchLikeService } from "../services/ResearchDetailService";

class ResearchDetailStore {
  public detail?: ResearchDetailData;
  public loading: boolean = false;
  public pdfHeight: number = 1000;

  constructor() {
    makeAutoObservable(this);
  }

  public async getData(id: string) {
    let response: ResearchDetailData;

    this.loading = true;
    try {
      response = await new ResearchDetailService(id).send();
    } catch (e: any) {
      this.loading = false;
      showError(e);
      return;
    }
    this.loading = false;
    this.detail = response;
    this.showPdf();
  }

  public showPdf() {

    let pdfjsLib = (window as any).pdfjsLib;
    let pdfjsViewer = (window as any).pdfjsViewer;
    pdfjsLib.GlobalWorkerOptions.workerSrc =
      "https://aoliapp.oss-cn-shenzhen.aliyuncs.com/pdf/pdf.worker.js";
    const CMAP_URL = "https://aoliapp.oss-cn-shenzhen.aliyuncs.com/pdf/cmaps/";
    const container = document.getElementById("viewerContainer");
    const eventBus = new pdfjsViewer.EventBus();
    const pdfLinkService = new pdfjsViewer.PDFLinkService({
      eventBus,
    });
    const pdfViewer = new pdfjsViewer.PDFViewer({
      container,
      eventBus,
      linkService: pdfLinkService,
      renderer: "svg",
      textLayerMode: 0,
    });
    pdfLinkService.setViewer(pdfViewer);

    eventBus.on("pagesinit", () => {
      pdfViewer.currentScaleValue = "page-width";
      let viewer = document.getElementById("viewer");
      this.pdfHeight = viewer?.clientHeight || 0;
      console.log("pagesinit", this.pdfHeight)
    });

    const loadingTask = pdfjsLib.getDocument({
      url: this.detail?.pdfurl.replace("http://","https://"),
      cMapUrl: CMAP_URL,
      cMapPacked: true,
    });
    loadingTask.promise.then((pdfDocument: any) => {
      pdfViewer.setDocument(pdfDocument);

      pdfLinkService.setDocument(pdfDocument, null);
      let viewer = document.getElementById("viewer");
      this.pdfHeight = viewer?.clientHeight || 0;
      console.log(" this.pdfHeight  this.pdfHeight ", this.pdfHeight)
    });

  }

  public async like() {
    if (this.detail) {
      this.detail.islike = this.detail.islike == 1 ? 0 : 1;
      let count = parseInt(this.detail.likecount);
      if (this.detail.islike == 1) {
        count += 1;
      } else {
        count += 1;
      }
      this.detail.likecount = count.toString();
    }
    try {
      await new ResearchLikeService(this.detail?.id, this.detail?.islike).send();
    } catch (e: any) {
      if (this.detail) {
        this.detail.islike = this.detail.islike == 1 ? 0 : 1;
        let count = parseInt(this.detail.likecount);
        if (this.detail.islike == 1) {
          count += 1;
        } else {
          count += 1;
        }
        this.detail.likecount = count.toString();
      }
      showError(e);
      return;
    }
  }

  public async follow() {
    if (this.detail) {
      this.detail.isfollow = this.detail.isfollow == 1 ? 0 : 1;
    }
    try {
      await new ResearchFollowService(this.detail?.id, this.detail?.isfollow).send();
    } catch (e: any) {
      if (this.detail) {
        this.detail.isfollow = this.detail.isfollow == 1 ? 0 : 1;
      }
      showError(e);
      return;
    }
  }




}

export { ResearchDetailStore };